package left_right_pointers;

public class BinarySearch {

    public int binarySearch(int[] arr, int target) {
        // 一左一右两个指针相向而行
        int left = 0, right = arr.length - 1;
        while (left <= right) {
            int mid = (right + left) / 2;
            if (arr[mid] == target) return mid;
            else if (arr[mid] < target) left = mid + 1;
            else right = mid - 1;
        }
        return -1;
    }
}
